﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

using System.Data.SqlClient;
using System.Data.Common;
/// <summary>
/// Summary description for CMIDB
/// </summary>
/// 
namespace CE.Promotion
{
    public class PromotionDB
    {
        public PromotionDB()
        {
        }

        public Int64 InsertUpdate(Promotion promotiondata)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocPromotionGGInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@PromotionId", promotiondata.PromotionId);
            _sPara.Add("@CEUserId", promotiondata.RefCEUserId);
            _sPara.Add("@promotionname", promotiondata.PromotionName);
            _sPara.Add("@username", promotiondata.UserName); 
            _sPara.Add("@email", promotiondata.Email);
            _sPara.Add("@mobile", promotiondata.Mobile);
            _sPara.Add("@commencementmonth", promotiondata.CommencementMonth);
            _sPara.Add("@IsExistCustomer", promotiondata.IsExistCustomer);
            _sPara.Add("@callat", promotiondata.CallAt);
            if (promotiondata.AgentId == 0)
            {
                _sPara.Add("@agentid", DBNull.Value);
            }
            else
            {
                _sPara.Add("@agentid", promotiondata.AgentId);
            }
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }


        public Promotion GetPromotion(Int64 PromotionId)
        {
            Promotion _promotion = null;
            MyDatabase _db = new MyDatabase();

            using (DataTable _dt = _db.GetDataTable("SELECT PromotionId,CEUserId,PromotionName,UserName,Email,Mobile,CommencementMonth,IsExistCustomer,CallAt,AgentId FROM tblPromotionGG WHERE PromotionId=" + PromotionId.ToString()))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _promotion = new Promotion();
                    _promotion = FillDataRecord(_dt, 0);
                }
            }
            return _promotion;
        }

        private Promotion FillDataRecord(DataTable myDataTable, int RowId)
        {
            Promotion myPromotion = new Promotion();

            myPromotion.PromotionId = Convert.ToInt64(myDataTable.Rows[RowId]["PromotionId"].ToString());
            myPromotion.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["CEUserId"].ToString());
            myPromotion.PromotionName = myDataTable.Rows[RowId]["PromotionName"].ToString();
            myPromotion.UserName = myDataTable.Rows[RowId]["UserName"].ToString();
            myPromotion.Email = myDataTable.Rows[RowId]["Email"].ToString();
            myPromotion.Mobile = myDataTable.Rows[RowId]["Mobile"].ToString();
            myPromotion.CommencementMonth = Convert.ToInt32(myDataTable.Rows[RowId]["CommencementMonth"].ToString());
            myPromotion.IsExistCustomer = Convert.ToInt32(myDataTable.Rows[RowId]["IsExistCustomer"].ToString());
            myPromotion.CallAt = Convert.ToInt32(myDataTable.Rows[RowId]["CallAt"].ToString());
            myPromotion.AgentId = Convert.ToInt64(myDataTable.Rows[RowId]["AgentId"].ToString());
            return myPromotion;
        }


    }
}
